﻿Imports System.Runtime.Serialization
Imports Framework

<CLSCompliant(True), Serializable(), DataContract>
Public Class CFileHash : Implements IComparable(Of CFileHash)

    <DataMember(Order:=1)> Public Name As String
    <DataMember(Order:=2)> Public MD5 As Guid

    Shared Sub New()
        Try
            CProto.Prepare(Of CFileHash)()
        Catch
        End Try
    End Sub

    Protected Sub New()
    End Sub

    Public Sub New(filePath As String)
        Name = IO.Path.GetFileName(filePath).ToLower
        MD5 = New Guid(CBinary.MD5(IO.File.ReadAllBytes(filePath)))
    End Sub
    Public Sub New(name As String, md5 As Guid)
        Me.Name = name.ToLower.Replace("\", "/")
        Me.MD5 = md5
    End Sub

    Public Overrides Function ToString() As String
        Dim base64 As String = CBinary.ToBase64(MD5)
        Return String.Concat(Name, " (", base64, ")")
    End Function

    Public Function CompareTo(other As CFileHash) As Integer Implements IComparable(Of CFileHash).CompareTo
        Return Me.Name.CompareTo(other.Name)
    End Function
End Class
